﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using MiniFox.Infrastructure;
using MiniFox.Platform.Applications.Auth;
using MiniFox.Platform.Resources;
using MiniFox.Resources;
using MiniFox.Utility;
using MiniFox.Web;
using MiniFox.Web.Infrastructure;
using System.ComponentModel.DataAnnotations;

namespace MiniFox.Platform.Applications.Web
{
    /// <summary>
    /// 认证代理
    /// </summary>
    [NoCache, AllowAnonymous]
    public class OAuthAgentController : MvcControllerBase
    {
        /// <summary>
        /// 获取当前令牌
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Response<string> GetToken()
        {
            var client = this.HttpContext.GetAuthClient<UserAuthClient>();
            return client.RefreshToken.CreateResponse();
        }
        /// <summary>
        /// 授权码统一回接地址，授权成功时，通过context取重写向地址并跳转
        /// </summary>
        /// <param name="authCode"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Connect([FromQuery(Name = OpenAuthKey.AUTH_CODE), Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired")] string authCode,
            [FromQuery(Name = OpenAuthKey.AUTH_CLIENT_CONTEXT), Required(ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_CommonRequired"), MaxLength(64)] string context)
        {
            IActionResult actionResult;
            var client = this.HttpContext.GetAuthClient<UserAuthClient>();
            var url = client.GetRedirectUrl(authCode, context);
            if (url == null)
            {
                actionResult = this.Content(ResMessage.ERR_InvalidAuthCode);
            }
            else
            {
                this.HttpContext.SetAuthClient(client);
                if (!string.Equals(url.GetFullHost(), Request.GetFullHost(), StringComparison.OrdinalIgnoreCase))
                    url = url.RemoveQueryString(ClientKey.SECURE_CLIENT_TOKEN).AddQueryString(ClientKey.SECURE_CLIENT_TOKEN, client.RefreshToken);
                actionResult = this.Redirect(url.ToString());
            }
            return actionResult;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Quit()
        {
            var client = this.HttpContext.GetAuthClient<UserAuthClient>();
            client.ClearState();
            var uri = client.SignOutUrl.AddQueryString(OpenAuthKey.AUTH_REDIRECT_URI, this.Request.GetFullHost());
            return this.Redirect(uri.ToString());
        }
        /// <summary>
        /// SPA客户端接入并发起跳转至sso
        /// </summary>
        /// <param name="redirectUri">完整的http地址</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Authorize([FromQuery(Name = OpenAuthKey.AUTH_REDIRECT_URI), RegularExpression(StringExtensions.PATTERN_URI, ErrorMessageResourceType = typeof(CommonMessages), ErrorMessageResourceName = "MSG_InvalidURI")] string redirectUri)
        {
            var client = this.HttpContext.GetAuthClient<UserAuthClient>();
            var url = client.GetAuthUrl(new Uri(redirectUri));

            return this.Redirect(url.ToString());
        }

    }
}
